package com.formiaomiao.demain.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;

import com.formiaomiao.demain.R;
import com.formiaomiao.demain.model.UserBean;
import com.formiaomiao.demain.util.MessageEvent;
import com.formiaomiao.demain.util.PreferenceUtils;
import com.formiaomiao.demain.util.PreferenceValues;
import com.formiaomiao.demain.util.RegexUtils;
import com.formiaomiao.demain.util.StaticValues;
import com.formiaomiao.demain.util.ToastUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class UpdateRefreshFragment extends DialogFragment {


    @BindView(R.id.et_update_username)
    EditText etUpdateUsername;
    @BindView(R.id.btn_update_submit)
    Button btnUpdateSubmit;
    Unbinder unbinder;

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        // request a window without the title
        dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.update_username_layout, container, false);
        unbinder = ButterKnife.bind(this, v);
        etUpdateUsername.setHint("请输入想要显示的文字，只支持英文和数字哦");
        return v;
    }

    @Override
    public void onStart() {
        super.onStart();
//        Dialog dialog = getDialog();
//        if (dialog != null) {
//            DisplayMetrics dm = new DisplayMetrics();
//            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
//            dialog.getWindow().setLayout((int) (dm.widthPixels * 0.8), (int) (dm.heightPixels * 0.3));
//        }
    }

    /**
     * 修改下拉刷新文字
     *
     * @param name
     */
    public void UpdataHead(String name) {
        PreferenceUtils.setString(getActivity(), PreferenceValues.REFRESHWORD, name);
        EventBus.getDefault().post(new MessageEvent("下拉刷新"));
        dismiss();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.btn_update_submit)
    public void onViewClicked() {
        if (etUpdateUsername.getText().toString().isEmpty()) {
            ToastUtils.show(getActivity(), "请输入想要显示的文字");
        } else if (RegexUtils.checkChinese(etUpdateUsername.getText().toString().trim())) {
            ToastUtils.show(getActivity(), "只能输入英文或数字哦");
        } else {
            UpdataHead(etUpdateUsername.getText().toString());
        }
    }

}
